package com.tony;

import java.io.*;

public class IODemo {
    /**
     * InputStream:
     *  BufferedInputStream ByteArrayInputStream DataInputStream FilterInputStream PushbackInputStream
     *  PipedInputStream ObjectInputStream
     * OutputStream:
     *  FileOutputStream、ByteArrayOutputStream、PipedOutputStream、BufferedOutputStream、ObjectOutputStream、FilterOutputStream、
     *  PrintStream、DataOutputStream
     *
     * Reader
     *  FileReader、CharArrayReader、PipedReader、StringReader、BufferedReader、InputStreamReader、FilterReader、PushbackReader
     * Writer
     *  FileWriter、CharArrayWriter、PipedWriter、StringWriter、BufferedWriter、OutputStreamWriter、FilterWriter、PrintWriter
     *
     *
     * @param args
     */
    public static void main(String[] args) {
        IODemo ioDemo = new IODemo();
        String path ="D:/IODEMO.txt";
        try {
            ioDemo.writeFile("12345",path);
            ioDemo.readFile(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void readFile(String path) throws Exception {
        FileInputStream fileInputStream = new FileInputStream(path);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");
        BufferedReader br =new BufferedReader(inputStreamReader);
        StringBuffer sb = new StringBuffer();
        String line ="";
        while((line=br.readLine()) !=null){
            sb.append(line).append("/n");
        }
        System.out.println(sb.toString());
        br.close();
    }

    public String readFile2(String path) throws Exception{
        FileInputStream fileInputStream = new FileInputStream(path);
        int size = fileInputStream.available();
        byte [] array = new byte[size];
        fileInputStream.read(array);
        return new String(array);
    }

    public void writeFile(String text,String path) throws Exception{
        File file = new File(path);
        file.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(text);
        bw.flush();
        bw.close();
        System.out.println("write file over!!!");
    }


}
